﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class Admin_Views_module_InsertProduct : System.Web.UI.UserControl
{
    productBus proBU = new productBus();
    UnitBU unitBU = new UnitBU();
    ManufacturerBus manuBU = new ManufacturerBus();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadUnitID();
            this.loadManuFac();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string nameProduct = txtProductName.Text;
        string desProduct = txtProDes.Text;
        int quantity = int.Parse(txtQuantity.Text.ToString());
        int unitID = int.Parse(cbxUnitID.SelectedItem.Value.ToString());
        string Igm = "~/images/"+lblFileInfo.Text;
        int manuFac = int.Parse(cbxManuFac.SelectedItem.Value.ToString());
        int price = int.Parse(txtprice.Text.ToString());
        bool status = true;
        if (!cbStatus.Checked)
        {
            status = false;
        }

        int result = proBU.insertProductBU(nameProduct, desProduct, quantity, unitID, Igm, manuFac, price, status);
        if (result != 0)
        {
            lblMesse.Text = "Insert OK";
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        lblFileInfo.Text = "";
        //txtmanufac.Text = "";
        txtprice.Text = "";
        txtProDes.Text = "";
        txtProductName.Text = "";
        txtQuantity.Text = "";
        // txtUnitID.Text = "";
    }

    public void LoadUnitID()
    {
        DataTable dt = new DataTable();
        dt = unitBU.getUnitBU();
        cbxUnitID.DataSource = dt;
        cbxUnitID.DataTextField = dt.Columns["unit_name"].ColumnName.ToString();
        cbxUnitID.DataValueField = dt.Columns["unit_id"].ColumnName.ToString();
        cbxUnitID.DataBind();
    }

    public void loadManuFac()
    {
        DataTable dt = new DataTable();
        dt = manuBU.getAllManufacturer();
        cbxManuFac.DataSource = dt;
        cbxManuFac.DataValueField = dt.Columns["manufacturer_id"].ColumnName.ToString();
        cbxManuFac.DataTextField = dt.Columns["name"].ColumnName.ToString();
        cbxManuFac.DataBind();
    }

    protected void cbxUnitID_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected bool checkFile(string fileName)
    {
        string ext = Path.GetExtension(fileName);
        switch (ext.ToLower())
        {
            case ".jpg":
                return true;
            case ".bmp":
                return true;
            default:
                return false;
        }
    }


    protected void Button3_Click(object sender, EventArgs e)
    {
        string file = "";

        if (fileUp.HasFile)
        {
            if (checkFile(fileUp.FileName))
            {
                try
                {
                    file = fileUp.FileName.ToString().Trim();
                    //Response.Write("dsds" + file);
                    if (File.Exists(Request.MapPath("~") + "\\images\\" + Server.UrlEncode(file)))
                    {
                        file = fileUp.FileName.ToString().Trim();
                        Image.Visible = true;
                        Image.ImageUrl = "~/images/" + Server.UrlEncode(file);

                        //Response.Write(file);
                    }
                    fileUp.SaveAs(Request.PhysicalApplicationPath + "images\\" + Server.UrlEncode(file));
                    Image.Visible = true;
                    Image.ImageUrl = "~/images/" + Server.UrlEncode(file);
                    lblFileInfo.Text = fileUp.FileName;
                }
                catch (System.Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }
    }
}